// src/router/index.js
import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Home.vue'
import Transitions from '../views/transitions/index.vue'
import ScssToCss from '../views/scss2css/index.vue'
import RpxPx from '../views/rpxpx/index.vue'
import Login from '../views/auth/Login.vue'
import WeChatTemplate from '../views/wechat/Template.vue'

const routes = [
  { path: '/login', name: 'Login', component: Login },
  { path: '/', name: 'Home', component: Home, meta: { requiresAuth: true } },
  { path: '/json-formatter', name: 'JSONFormatter', component: Transitions, meta: { requiresAuth: true } },
  { path: '/scss-to-css', name: 'ScssToCss', component: ScssToCss, meta: { requiresAuth: true } },
  { path: '/rpx-px', name: 'RpxPxConverter', component: RpxPx, meta: { requiresAuth: true } },
  { path: '/wechat-template', name: 'WeChatTemplate', component: WeChatTemplate, meta: { requiresAuth: true } },
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

import { useAuthStore } from '../stores/auth'
router.beforeEach((to) => {
  const auth = useAuthStore()
  if (to.path === '/login') return true
  if (to.meta?.requiresAuth && !auth.isAuthenticated) {
    return '/login'
  }
  return true
})

export default router